package org.eece262.taskshareserver;

import java.util.LinkedList;

import com.google.appengine.api.datastore.Key;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.edu.eece262.taskshare.Messages;

@PersistenceCapable
public class Task {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    
    @Persistent long taskGlobalID;
    
    @Persistent
    private int taskLocalID;
    
    @Persistent
	private String taskName;
    
    @Persistent
    private String taskOwner;
    
    @Persistent
    private LinkedList<String> taskAssignedUsernames;
	
    @Persistent
    private String taskNotes;
    
    @Persistent
    private String taskLocation;
	
    @Persistent
    private long taskGlobalParentID;
	
    @Persistent
    private int taskDueDate;
	
    @Persistent
    private int taskDueTime;
	
    @Persistent
    private int taskPercentComplete;
	
    @Persistent
    private int taskDone;
	
    @Persistent
    private int taskPriority;
    
    @Persistent
    private boolean taskDeleted;
    
    public Task(Messages.Task task, String user, boolean createGlobalID) {
    	update(task);
    	this.taskOwner = user;
    	if(createGlobalID)
    		this.taskGlobalID = System.currentTimeMillis();
    	else
    		this.taskGlobalID = task.getGlobal_id();
    	this.taskDeleted = false;
    	this.taskAssignedUsernames = task.getAssignedList();
    }
    
    public Key getKey() {
        return key;
    }

	public int getLocal_id() {
		return taskLocalID;
	}

	public void setLocal_id(int localId) {
		taskLocalID = localId;
	}

	public String getName() {
		return taskName;
	}

	public void setName(String name) {
		this.taskName = name;
	}
	
	public String getOwner() {
		return taskOwner;
	}
	
	public void setOwner(String user) {
		this.taskOwner = user;
	}

	public String getNotes() {
		return taskNotes;
	}

	public void setNotes(String notes) {
		this.taskNotes = notes;
	}

	public String getLocation() {
		return taskLocation;
	}

	public void setLocation(String location) {
		this.taskLocation = location;
	}

	public long getGlobal_parent() {
		return taskGlobalParentID;
	}

	public void setGlobal_parent(long globalParent) {
		taskGlobalParentID = globalParent;
	}

	public int getDue_date() {
		return taskDueDate;
	}

	public void setDue_date(int dueDate) {
		taskDueDate = dueDate;
	}

	public int getDue_time() {
		return taskDueTime;
	}

	public void setDue_time(int dueTime) {
		taskDueTime = dueTime;
	}

	public int getPercent_complete() {
		return taskPercentComplete;
	}

	public void setPercent_complete(int percentComplete) {
		taskPercentComplete = percentComplete;
	}

	public int getDone() {
		return taskDone;
	}

	public void setDone(int done) {
		this.taskDone = done;
	}

	public int getPriority() {
		return taskPriority;
	}

	public void setPriority(int priority) {
		this.taskPriority = priority;
	}
	
	public Messages.Task getMessagesTask() {
		Messages.Task rtnValue = new Messages.Task()
			.setGlobal_id(taskGlobalID)
			.setLocal_id(taskLocalID)
			.setName(taskName)
			.setNotes(taskNotes)
			.setLocation(taskLocation)
			.setGlobal_parent(taskGlobalParentID)
			.setDue_date(taskDueDate)
			.setDue_time(taskDueTime)
			.setPercent_complete(taskPercentComplete)
			.setDone(taskDone)
			.setPriority(taskPriority)
			.setAssignedList(taskAssignedUsernames);
		return rtnValue;
	}

	public void update(Messages.Task task) {
		taskLocalID = task.getLocal_id();
    	taskName = task.getName();
    	taskNotes = task.getNotes();
    	taskLocation = task.getLocation();
    	taskGlobalParentID = task.getGlobal_parent();
    	taskDueDate = task.getDue_date();
    	taskDueTime = task.getDue_time();
    	taskPercentComplete = task.getPercent_complete();
    	taskDone = task.getDone();
    	taskPriority = task.getPriority();
    	taskAssignedUsernames = task.getAssignedList();
	}

	public long getGlobalID() {
		return taskGlobalID;
	}

	public void setGlobalID(long taskGlobalID) {
		this.taskGlobalID = taskGlobalID;
	}

	public void setAssignedUsernames(LinkedList<String> taskAssignedUsernames) {
		this.taskAssignedUsernames = taskAssignedUsernames;
	}
	
	public Task addAssignedUsernames(String user) {
		this.taskAssignedUsernames.add(user);
		return this;
	}

	public LinkedList<String> getAssignedUsernames() {
		return taskAssignedUsernames;
	}
	
	public LinkedList<String> getUsersAffected() {
		LinkedList<String> affected = new LinkedList<String>();
		affected.add(taskOwner);
		affected.addAll(taskAssignedUsernames);
		return affected;
	}

	public void setTaskDeleted(boolean taskDeleted) {
		this.taskDeleted = taskDeleted;
	}

	public boolean isTaskDeleted() {
		return taskDeleted;
	}
}
